<?php
/* -------------------------------------------------------------------------------------
* 	ID:						$Id: contact_us.php 504 2014-01-31 16:40:24Z phone.mueller@googlemail.com $
* 	Letzter Stand:			$Revision: 504 $
* 	zuletzt geaendert von:	$Author: $
* 	Datum:					$Date: 2014-01-31 16:40:24 +0000 (Fri, 31 Jan 2014) $
*
* 	extends by dirk.mueller (edv-mueller.com)
* 	http://www.itdotmedia.de
*
* --------------------------------------------------------------------------------------
* 	based on:
* 	SEO:mercari by Siekiera Media
* 	http://www.seo-mercari.de
* 	Copyright (c) since 2011 SEO:mercari
* 	(c) 2000-2001 The Exchange Project  (earlier name of osCommerce)
* 	(c) 2002-2003 osCommerce - www.oscommerce.com
* 	(c) 2003     nextcommerce - www.nextcommerce.org
* 	(c) 2005     xt:Commerce - www.xt-commerce.com
*
* 	Released under the GNU General Public License
* ----------------------------------------------------------------------------------- */
$error = false;

$checkbox_contact_items = array(
  'mail'  => 'per Email',
  'phone' => 'per Telefon',
  'date'  => 'Aussentermin'
);
$checkbox_catalog_items = array(
  'furniture' => 'B&uuml;erom&ouml;bel',
  'chair'     => 'B&uuml;rost&uuml;hle'
);

if(isset ($_POST['action']) && ($_POST['action'] == 'send')) {

  if(SHOW_DSG_ON_CONTACT_FORM == 'true' && $_POST['datensg'] != 'yes') {
    $error = true;
    $message_stack->add(ERROR_CDATENSG, 'error');
  }

	if((SHOW_CAPTCHA_ON_CONTACT_FORM == 'true') && (empty($_POST['security_code']) || ($_POST['security_code'] != $_SESSION['security_code_shop_content']))) {
    $error = true;
		$message_stack->add(TEXT_CAPTCHA_ERROR, 'error');
	}

	if(empty($_POST['customer_company'])
      || empty($_POST['street'])
      || empty($_POST['zip_city'])) {
    $error = true;
		$message_stack->add(ERROR_EMPTIES, 'error');
  }

	if(!validate_email(trim($_POST['email']))) {
		$error = true;
		$message_stack->add(ERROR_MAIL, 'error');
	}

		if(!$error) {
			$smarty->assign('MAIL_TEXT', nl2br(strip_tags($_POST['message_body'])));
    	$smarty->assign('INPUT_COMPANY', strip_tags($_POST['customer_company']));
      $smarty->assign('INPUT_NAME', strip_tags($_POST['customer_name']));
      $smarty->assign('INPUT_EMAIL', strip_tags($_POST['email']));
      $smarty->assign('INPUT_PHONE', strip_tags($_POST['phone']));
      $smarty->assign('INPUT_STREET', strip_tags($_POST['street']));
      $smarty->assign('INPUT_ZIP_CITY', strip_tags($_POST['zip_city']));
      
      if (isset($_POST['contact'])) {
        $CHECKBOX_CONTACT = array();
        foreach($_POST['contact'] as $k) $CHECKBOX_CONTACT[] = $checkbox_contact_items[$k];
        $CHECKBOX_CONTACT = implode(', ', $CHECKBOX_CONTACT);
      }
      else
        $CHECKBOX_CONTACT = '-';
      $smarty->assign('CHECKBOX_CONTACT', $CHECKBOX_CONTACT);

      if (isset($_POST['catalog'])) {
        $CHECKBOX_CATALOG = array();
        foreach($_POST['catalog'] as $k) $CHECKBOX_CATALOG[] = $checkbox_catalog_items[$k];
        $CHECKBOX_CATALOG = implode(', ', $CHECKBOX_CATALOG);
      }
      else
        $CHECKBOX_CATALOG = '-';
      $smarty->assign('CHECKBOX_CATALOG', $CHECKBOX_CATALOG);

			$smarty->assign('ASK_NAME', $_POST['customer_name']);

			$smarty->caching = false;
			require_once(DIR_FS_INC.'inc.get_mail_body.php');
			$html_mail .= $smarty->fetch('html:contact');
			$html_mail .= $signatur_html;
			$txt_mail .= $smarty->fetch('txt:contact');
			$txt_mail .= $signatur_text;
			require_once(DIR_FS_INC.'inc.get_mail_data.php');
			$mail_data = get_mail_data('contact');

			$contact_subject = str_replace('{$date}', strftime(DATE_FORMAT_LONG), $mail_data['EMAIL_SUBJECT']);
			$contact_subject = str_replace('{$shop_besitzer}', STORE_OWNER, $contact_subject);
			$contact_subject = str_replace('{$shop_name}', STORE_NAME, $contact_subject);

			php_mail($_POST['email'],
						$_POST['customer_name'],
						$mail_data['EMAIL_ADDRESS'],
						$mail_data['EMAIL_ADDRESS_NAME'],
						$mail_data['EMAIL_FORWARD'],
						$_POST['email'],
						$_POST['customer_name'],
						'',
						'',
						$contact_subject,
						$html_mail,
						$txt_mail);

			unset($_SESSION['security_code_shop_content']);
			unset($_SESSION['captcha']);
			redirect(href_link(FILENAME_CONTENT, 'action=success&coID='.(int)$_GET['coID']));
		}
		if($message_stack->size > 0)
			$get[] = 'msg=1';
}

$smarty->assign('CONTACT_HEADING', $shop_content_data->fields['content_title']);
if (isset ($_GET['action']) && ($_GET['action'] == 'success')) {
  $smarty->assign('success', '1');
	$smarty->assign('BUTTON_CONTINUE', '<a href="'.href_link(FILENAME_DEFAULT).'">'.image_button('button_continue.gif', IMAGE_BUTTON_CONTINUE).'</a>');
}

else {
  $contact_content = $shop_content_data->fields['content_text'];
  
  require (DIR_WS_INCLUDES.'header.php');
	$smarty->assign('CONTACT_CONTENT', $contact_content);
	$smarty->assign('FORM_ACTION', draw_form('contact_us', href_link(FILENAME_CONTENT, 'coID='.(int)$_GET['coID'])).draw_hidden_field('action', 'send'));

  function get_customer_info($customer_id) {
		global $db;
  	$customer_info = $db->db_query(
      "SELECT
        customers_gender, customers_cid, customers_vat_id, customers_vat_id_status,
        customers_firstname, customers_lastname, customers_dob,
        customers_email_address, customers_telephone, customers_fax,
        a.entry_company,
        a.entry_street_address, a.entry_postcode, a.entry_city, a.entry_state
      FROM
        ".TABLE_CUSTOMERS." c
        inner join ".TABLE_ADDRESS_BOOK." a on a.customers_id = c.customers_id
      WHERE
        c.customers_id = '".$customer_id."'
      LIMIT 1");
    return $customer_info->fields;
	}

  $contact_company = '';
  $contact_name = '';
  $contact_email = '';
	$contact_phone = '';
	$contact_street = '';
	$contact_zip = '';
	$contact_city = '';

  if(isset($_SESSION['customer_id'])) {
    $customer_info = get_customer_info((int)($_SESSION['customer_id']));
		$contact_company = $customer_info['entry_company'];
		$contact_name = $customer_info['customers_firstname'].' '.$customer_info['customers_lastname'];
		$contact_email = $customer_info['customers_email_address'];
		$contact_phone = $customer_info['customers_telephone'];
		$contact_street = $customer_info['entry_street_address'];
		$contact_zip = $customer_info['entry_postcode'];
		$contact_city = $customer_info['entry_city'];
	}

	$smarty->assign('INPUT_COMPANY', draw_input_field('customer_company', $contact_company));
	$smarty->assign('INPUT_NAME', draw_input_field('customer_name', $contact_name));
	$smarty->assign('INPUT_EMAIL', draw_input_field('email', ($error ? $_POST['email'] : $contact_email)));
	$smarty->assign('INPUT_PHONE', draw_input_field('phone', ($error ? $_POST['phone'] : $contact_phone)));
	$smarty->assign('INPUT_STREET', draw_input_field('street', ($error ? $_POST['street'] : $contact_street)));
	$smarty->assign('INPUT_ZIP_CITY', draw_input_field('zip_city', ($error ? $_POST['zip_city'] : $contact_zip.($contact_city!='' ? ' / ':'').$contact_city)));

  foreach(array_keys($checkbox_contact_items) as $item)
  	$smarty->assign('CHECKBOX_CONTACT_'.strtoupper($item), draw_checkbox_field('contact[]', $item, ($error ? isset($_POST['contact']) && in_array($item, $_POST['contact']) : false), 'class="styled"'));
  foreach(array_keys($checkbox_catalog_items) as $item)
  	$smarty->assign('CHECKBOX_CATALOG_'.strtoupper($item), draw_checkbox_field('catalog[]', $item, ($error ? isset($_POST['catalog']) && in_array($item, $_POST['catalog']) : false), 'class="styled"'));

	$smarty->assign('INPUT_TEXT', draw_textarea_field('message_body', 'soft', 50, 15, $_POST['message_body']));
	$smarty->assign('BUTTON_SUBMIT', image_submit('button_continue.gif', IMAGE_BUTTON_SEND));

	if(SHOW_CAPTCHA_ON_CONTACT_FORM == 'true') {
		$captcha_site = '_shop_content';
		include('captcha.php');
		$smarty->assign('VVIMG', '<img width="200" height="70" src="captcha.php?show=true&name=shop_content" alt="Captcha" id="captcha_image" /> ');
		$smarty->assign('INPUT_CAPTCHA', draw_input_field('security_code', '', 'size="6" maxlength="6" style="width:25px; text-align:center" id="security_code"', 'text', false));
	}

	if(SHOW_DSG_ON_CONTACT_FORM == 'true') {
		$dsg_text = (SITE_OVERLAYS == 'true' ? sprintf(INFO_DSG_OVERLAY, href_link(FILENAME_POPUP_CONTENT, 'coID=2&iframe=true')) : sprintf(INFO_DSG_POPUP, href_link(FILENAME_CONTENT, 'coID=2')));
		$smarty->assign('DATENSG', draw_checkbox_field('datensg', 'yes').' '.$dsg_text);
	}

	$smarty->assign('FORM_END', '</form>');
}

$smarty->assign('language', $_SESSION['language']);

$smarty->caching = false;
$main_content = $smarty->fetch(CURRENT_TEMPLATE.'/module/contact_us.html');
